<?php
class users extends spController
{

	function index(){ // 这里是首页
		$this->tpl_title = "用户和权限";
		$this->results = spClass("lib_users")->spPager($this->spArgs('page', 1), 10)->findAll("","uid DESC"); 
		$this->pager = spClass("lib_users")->spPager()->getPager();
		$this->display("admin/users_index.html");		
	}

	function write(){ // 新建用户
		$this->tpl_title = "新建用户";
		$this->formUrl = spUrl("users", "add");
		import("spAcl.php"); // 引入Acl文件，使得可以生成加密的密码输入框
		$this->display("admin/users_write.html");
	}

	function edit(){ // 修改
		$this->tpl_title = "编辑用户及权限";
		$this->formUrl = spUrl("users", "update");
		import("spAcl.php"); // 引入Acl文件，使得可以生成加密的密码输入框
		// 这里先判断是否传入了uid
		if( $uid = $this->spArgs("uid") ){
			// 查找表，获取信息
			$this->info = spClass("lib_users")->find(array('uid'=>$uid));
			$this->display("admin/users_edit.html");
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("users","index"));
		}
	}

	function add(){ // 增加
		import("spAcl.php"); // 引入Acl文件，使得可以生成加密的密码输入框
		$users = spClass("lib_users");
		$password = spClass('md5password')->pwvalue();// 通过acl的pwvalue获取提交的加密密码
		$newrow = array( // 这里制作新增记录的值
			'name' => $this->spArgs('name'), 
			'password' => $password,
			'mail' => $this->spArgs('mail'),
			'created' => mktime(),
			'nickName' => $this->spArgs('nickName'),
			'phone' => $this->spArgs('phone'),
			'status' => $this->spArgs('status'),
			'acl' => $this->spArgs('acl'),
		);
		$users->create($newrow); 
		$this->success("新用户添加成功！", spUrl("users", "index"));
	}

	function update(){ //更新
		import("spAcl.php"); // 引入Acl文件，使得可以生成加密的密码输入框
		$post = spClass("lib_users");
		// 这里先判断是否传入了id
		if( $uid = $this->spArgs("uid") ){
		$conditions = array("uid"=>$uid); 
		$password = spClass('md5password')->pwvalue();// 通过acl的pwvalue获取提交的加密密码
		// 如果密码为空则不更新
		if($password == "-1" or $password == "0"){
		$newrow = array(
			'name' => $this->spArgs('name'), 
			'mail' => $this->spArgs('mail'),
			'nickName' => $this->spArgs('nickName'),
			'phone' => $this->spArgs('phone'),
			'acl' => $this->spArgs('acl'),
			'status' => $this->spArgs('status'),
			);
		}else{
		$newrow = array(
			'name' => $this->spArgs('name'), 
			'mail' => $this->spArgs('mail'),
			'password' => $password,
			'nickName' => $this->spArgs('nickName'),
			'phone' => $this->spArgs('phone'),
			'acl' => $this->spArgs('acl'),
			'status' => $this->spArgs('status'),
			);
		}
		$post->update($conditions, $newrow); // 更新记录
		$this->success("更新用户信息成功！", spUrl("users", "index"));
		}
	}

	function del(){// 删除用户
		// 这里先判断是否传入了uid
		if( $uid = $this->spArgs("uid") ){
			// 无法删除自己
			/* if ($uid = $_SESSION["userinfo"]["uid"]){
				$this->error("无法删除自己哦！", spUrl("users","index"));
			}	 */		
			spClass("lib_users")->delete(array('uid'=>$uid));
			$this->success("删除成功！", spUrl("users","index"));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("users","index"));
		}
	}

}	